/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import "../../../../shared/qml" as Shared
import WebFeeds 1.0
import GoogleReader 1.0

Shared.ComboBoxMenuItem {
    id: root
    property int __sortType: root.pageItem ? root.pageItem.service.itemSortType : GoogleReaderEnum.IstSortByNewest
    property variant service: root.pageItem ? root.pageItem.service : undefined
    property PageItem pageItem: menu.pageItem

    text: "Sort by"
    selectionText: (root.__sortType != GoogleReaderEnum.IstSortByNewest) ?
                       ((root.__sortType != GoogleReaderEnum.IstSortByOldest) ? "Magic" : "Oldest")
                     : "Newest"

    onClicked: {
        menu.finished = false;
        sortSelectionDialog.open();
    }

    SelectionDialog {
        id: sortSelectionDialog
        titleText: qsTr("Sort by")
        model: ListModel {
            ListElement {name: "Newest";}
            ListElement {name: "Oldest";}
            ListElement {name: "Magic";}
        }
        onStatusChanged: {
            if((sortSelectionDialog.status == DialogStatus.Opening)
               || (sortSelectionDialog.status == DialogStatus.Open)) {
                sortSelectionDialog.selectedIndex = root.__sortType;
            }
        }
        onAccepted: {
            if((sortSelectionDialog.selectedIndex >= 0)
                && (root.service.itemSortType != sortSelectionDialog.selectedIndex)) {
                root.service.itemSortType = sortSelectionDialog.selectedIndex;
                root.service.refreshFeed(root.pageItem);
            }
            menu.finished = true;
        }
        onRejected: menu.finished = true;
    }
}
